from rest_framework import serializers
from user.models import UserModel
# 用户信息序列化器
class UserModelSerializer(serializers.ModelSerializer):
    pic_url = serializers.ImageField(use_url=True, required=False)

    class Meta:
        model = UserModel
        fields = ('id', 'email', 'mobile', 'nikename', 'pic_url', 'tscore')

    def update(self, instance, validated_data):
        # 从 `validated_data` 中获取 pic_url（图片文件）
        pic_url = validated_data.get('pic_url', None)
        instance = super().update(instance, validated_data)

        # 如果上传了新文件，更新 pic_url 字段
        if pic_url:
            instance.pic_url = pic_url

        instance.save()
        return instance





